<template>
  <div class="login-container">
    <div class="login-background"></div>
    <div class="login-content">
      <div class="login-box">
        <div class="login-header">
          <h1 class="login-title">BoomShake</h1>
          <p class="login-subtitle">管理后台</p>
        </div>
        
        <form @submit.prevent="handleLogin" class="login-form">
          <div class="form-group">
            <div class="input-container">
              <svg class="input-icon" viewBox="0 0 24 24">
                <path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>
              </svg>
              <input
                v-model="form.username"
                type="text"
                placeholder="请输入账号"
                class="form-input"
                required
              />
            </div>
          </div>
          
          <div class="form-group">
            <div class="input-container">
              <svg class="input-icon" viewBox="0 0 24 24">
                <path d="M18,8h-1V6c0-2.76-2.24-5-5-5S7,3.24,7,6v2H6c-1.1,0-2,0.9-2,2v10c0,1.1,0.9,2,2,2h12c1.1,0,2-0.9,2-2V10C20,8.9,19.1,8,18,8z M12,17c-1.1,0-2-0.9-2-2s0.9-2,2-2s2,0.9,2,2S13.1,17,12,17z M15.1,8H8.9V6c0-1.71,1.39-3.1,3.1-3.1s3.1,1.39,3.1,3.1V8z"/>
              </svg>
              <input
                v-model="form.password"
                :type="showPassword ? 'text' : 'password'"
                placeholder="请输入密码"
                class="form-input"
                required
              />
              <button
                type="button"
                @click="showPassword = !showPassword"
                class="password-toggle"
              >
                <svg v-if="showPassword" viewBox="0 0 24 24">
                  <path d="M12,9A3,3 0 0,0 9,12A3,3 0 0,0 12,15A3,3 0 0,0 15,12A3,3 0 0,0 12,9M12,17A5,5 0 0,1 7,12A5,5 0 0,1 12,7A5,5 0 0,1 17,12A5,5 0 0,1 12,17M12,4.5C7,4.5 2.73,7.61 1,12C2.73,16.39 7,19.5 12,19.5C17,19.5 21.27,16.39 23,12C21.27,7.61 17,4.5 12,4.5Z"/>
                </svg>
                <svg v-else viewBox="0 0 24 24">
                  <path d="M11.83,9L15,12.16C15,12.11 15,12.05 15,12A3,3 0 0,0 12,9C11.94,9 11.89,9 11.83,9M7.53,9.8L9.08,11.35C9.03,11.56 9,11.77 9,12A3,3 0 0,0 12,15C12.22,15 12.44,14.97 12.65,14.92L14.2,16.47C13.53,16.8 12.79,17 12,17A5,5 0 0,1 7,12C7,11.21 7.2,10.47 7.53,9.8M2,4.27L4.28,6.55L4.73,7C3.08,8.3 1.78,10 1,12C2.73,16.39 7,19.5 12,19.5C13.55,19.5 15.03,19.2 16.38,18.66L16.81,19.09L19.73,22L21,20.73L3.27,3M12,7A5,5 0 0,1 17,12C17,12.64 16.87,13.26 16.64,13.82L19.57,16.75C21.07,15.5 22.27,13.86 23,12C21.27,7.61 17,4.5 12,4.5C10.6,4.5 9.26,4.75 8,5.2L10.17,7.35C10.76,7.13 11.37,7 12,7Z"/>
                </svg>
              </button>
            </div>
          </div>
          
          <button
            type="submit"
            :disabled="loading"
            class="login-button"
          >
            <svg v-if="loading" class="loading-icon" viewBox="0 0 24 24">
              <path d="M12,4V2A10,10 0 0,0 2,12H4A8,8 0 0,1 12,4Z"/>
            </svg>
            <span v-if="!loading">登录</span>
            <span v-else>登录中...</span>
          </button>
        </form>
        
        <div class="login-footer">
          <div class="test-credentials">
            <p class="test-title">测试账号</p>
            <div class="credential-item">
              <span class="credential-label">管理员：</span>
              <span class="credential-value">admin / 123456</span>
            </div>
          </div>
          <p class="footer-text">
            © 2024 BoomShake. 版权所有
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { login } from '../api/auth'
import { message } from '../utils/message'

const router = useRouter()

const form = reactive({
  username: '',
  password: ''
})

const loading = ref(false)
const showPassword = ref(false)

const handleLogin = async () => {
  if (!form.username || !form.password) {
    message.warning('请填写账号和密码')
    return
  }
  
  loading.value = true
  
  try {
    const response = await login({
      username: form.username,
      password: form.password
    })
    
    if (response.code === 200) {
      // 保存token和用户信息
      localStorage.setItem('admin_token', response.data.token)
      localStorage.setItem('admin_info', JSON.stringify(response.data.adminInfo))
      
      message.success('登录成功，正在跳转...')
      
      // 延迟跳转，让用户看到成功提示
      setTimeout(async () => {
        await router.push('/admin/dashboard')
      }, 1000)
    } else {
      message.error(response.message || '登录失败')
    }
  } catch (error) {
    console.error('登录错误:', error)
    if (error.response?.data?.message) {
      message.error(error.response.data.message)
    } else {
      message.error('登录失败，请检查网络连接')
    }
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.login-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: url('../assets/background.png') no-repeat center center;
  background-size: cover;
  background-attachment: fixed;
  filter: blur(1px);
  z-index: 1;
}

.login-background::before {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: linear-gradient(
    135deg,
    rgba(88, 86, 214, 0.8),
    rgba(139, 69, 19, 0.3),
    rgba(34, 139, 34, 0.7)
  );
  z-index: 2;
}

.login-content {
  position: relative;
  z-index: 10;
  width: 100%;
  max-width: 420px;
  padding: 20px;
}

.login-box {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 20px;
  padding: 40px 30px;
  box-shadow: 
    0 20px 40px rgba(88, 86, 214, 0.3),
    0 10px 20px rgba(34, 139, 34, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-title {
  font-size: 2.5rem;
  font-weight: 700;
  background: linear-gradient(135deg, #5856d6, #228B22);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin: 0;
  letter-spacing: -0.5px;
}

.login-subtitle {
  color: #666;
  font-size: 1.1rem;
  margin: 8px 0 0 0;
  font-weight: 500;
}

.login-form {
  margin-bottom: 30px;
}

.form-group {
  margin-bottom: 24px;
}

.input-container {
  position: relative;
  display: flex;
  align-items: center;
}

.input-icon {
  position: absolute;
  left: 15px;
  width: 20px;
  height: 20px;
  fill: #888;
  z-index: 2;
}

.form-input {
  width: 100%;
  padding: 16px 16px 16px 45px;
  border: 2px solid #e1e5e9;
  border-radius: 12px;
  font-size: 1rem;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.8);
  outline: none;
}

.form-input:focus {
  border-color: #5856d6;
  box-shadow: 0 0 0 4px rgba(88, 86, 214, 0.1);
  background: rgba(255, 255, 255, 0.95);
}

.form-input:focus + .input-icon {
  fill: #5856d6;
}

.password-toggle {
  position: absolute;
  right: 15px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
  transition: background-color 0.2s ease;
}

.password-toggle:hover {
  background-color: rgba(88, 86, 214, 0.1);
}

.password-toggle svg {
  width: 20px;
  height: 20px;
  fill: #888;
}

.login-button {
  width: 100%;
  padding: 16px;
  background: linear-gradient(135deg, #5856d6, #228B22);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  position: relative;
  overflow: hidden;
}

.login-button:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(88, 86, 214, 0.4);
}

.login-button:active:not(:disabled) {
  transform: translateY(0);
}

.login-button:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.loading-icon {
  width: 20px;
  height: 20px;
  fill: currentColor;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.login-footer {
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid rgba(0, 0, 0, 0.1);
}

.test-credentials {
  margin-bottom: 15px;
  padding: 12px;
  background: rgba(88, 86, 214, 0.05);
  border-radius: 8px;
  border: 1px solid rgba(88, 86, 214, 0.1);
}

.test-title {
  font-size: 0.8rem;
  color: #5856d6;
  font-weight: 600;
  margin: 0 0 8px 0;
}

.credential-item {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
}

.credential-label {
  font-size: 0.8rem;
  color: #666;
  font-weight: 500;
}

.credential-value {
  font-size: 0.8rem;
  color: #333;
  font-weight: 600;
  font-family: 'Courier New', monospace;
  background: rgba(255, 255, 255, 0.8);
  padding: 2px 6px;
  border-radius: 4px;
  border: 1px solid rgba(88, 86, 214, 0.2);
}

.footer-text {
  color: #888;
  font-size: 0.9rem;
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-content {
    padding: 10px;
  }
  
  .login-box {
    padding: 30px 20px;
  }
  
  .login-title {
    font-size: 2rem;
  }
  
  .login-subtitle {
    font-size: 1rem;
  }
}

/* 动画效果 */
.login-box {
  animation: fadeInUp 0.6s ease-out;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.form-group {
  animation: fadeInUp 0.6s ease-out;
  animation-fill-mode: both;
}

.form-group:nth-child(1) { animation-delay: 0.1s; }
.form-group:nth-child(2) { animation-delay: 0.2s; }
.login-button { 
  animation: fadeInUp 0.6s ease-out 0.3s;
  animation-fill-mode: both;
}
</style> 